package Tree;

import java.util.Deque;
import java.util.LinkedList;

public class MinDepth {
    public static void main(String[] args) {

    }
    public int minDepth(TreeNode root){
        if(root == null) return 0;
        Deque<TreeNode> deque = new LinkedList<>();
        int depth = 0;
        deque.add(root);
        while(!deque.isEmpty()){
            int size = deque.size();
            depth++;
            for(int i = 0; i < size; i++){
                TreeNode node = deque.poll();
                if(node.left != null) deque.offer(node.left);
                if(node.right != null) deque.offer(node.right);
                if(node.left == null && node.right == null) return depth;
            }
        }
        return depth;
    }
}
